/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.agent;

import jade.core.AID;
import ca.usherbrooke.behaviours.controller.request.SubscriberWorldModel;
import ca.usherbrooke.model.world.IWorldModel;

/**
 * @author Cody Stoutenburg
 * 
 */
public abstract class WorldUpdaterAgent extends BasicAgent {
	private static final long serialVersionUID = 1L;
	protected IWorldModel world;

	protected AID worldAgent;

	private SubscriberWorldModel subscription;

	@Override
	protected void setup() {
		super.setup();

		worldAgent = (AID) getArguments()[argumentsIdx + 0];
		subscription = new SubscriberWorldModel(this, worldAgent);
		this.addBehaviour(subscription);

		argumentsIdx += 1;
	}

	public final AID getWorldAgent() {
		return this.worldAgent;
	}

	public void setWorldAID(AID worldAgent) {
		this.worldAgent = worldAgent;
		subscription.unsubscribe();
		this.removeBehaviour(subscription);
		subscription = new SubscriberWorldModel(this, worldAgent);
		this.addBehaviour(subscription);
	}

	public final void setWorld(IWorldModel world) {
		if (this.world == null) {
			this.world = world;
			initWorld();
		}
		this.world = world;
	}

	protected abstract void initWorld();

	@Override
	protected void takeDown() {
		super.takeDown();
		subscription.unsubscribe();
	}

}
